<?php
/**
 * Add a user to the mailbox
 * 
 * 
 */
use Fox\Behaviors;
use Orx\User\MailBox;
use Fox\Dao\SqlFragment;
use Fox\Html\Input;
use Fox\Html\TextArea;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\Html\DropList;
use Fox\Html\Screen;

$xview = Xview::instance();
$inlineId = $xview->val('inlineid');
$excludedValues = array();
try {
	$mailbox = Fox\Dao::load(MailBox::className(), $inlineId);
	foreach($mailbox->sharedUsers() as $sharedUser)  {
		$excludedValues[] = $sharedUser->shared_id;
	}
	$excludedValues[] = $mailbox->idusr;
} catch(Fox\Object\Exception\NotExist $e) {
	$xview->systemError();
	exit;
}

$searchHelp = new Fox\Html\SearchHelp('Users');
$searchHelp->width(500);
$searchHelp->setMultiple(new Fox\Html\Button('select',"ReloadFromSelectLines('#b_GRUsers','MC_gTarget',
												'?mod=messages&act=mailbox&sac=shared_users&inlineid=$inlineId','App.close(\'sh:Users\')');"));
/*
$searchHelp->setMultiple(new Fox\Html\Button('select'))
				->behaviors(Behaviors::action(
					Generator::url('user')
				));
*/
$searchHelp->switchToObjectMode();
$searchHelp->getSelBlock()->item('email')->size(Input::SIZE_MEDIUM);

$searchHelp->grid()->fieldcat()->field('idusr')->hidden();

if(isset($_POST['dbcnt']))
{
	$inputs = $searchHelp->screen()->get();
	$searchHelp->screen()->prefix('t',true);
	$fragments[] = $searchHelp->screen()->fragments();
	$finder = new User();
	$fragments[] = new \Fox\Dao\SqlFragment('t.idusr NOT IN (?) ',array($excludedValues),array(Connection::PARAM_STR_ARRAY));
	$fragments[] = new SqlFragment('w.webid = ? AND t.cloudname = ?',array(Xview::instance()->id(),User::current()->cloudname()));
	
	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),
						array('fname' => false),
						array(User::$JOIN_WEBAPPS),
						$inputs['dbcnt']);
	$searchHelp->grid()->setDatas($res);
	echo $searchHelp->grid()->Display();
}
else
{
	return $searchHelp->display();
}